//
//  NSString+NBL.m
//  NBLiOSTools
//
//  Created by GangX-yangjh on 2021/12/5.
//

#import "NSString+NBL.h"
#import "UIColor+NBL.h"
#import "NSURL+NBL.h"

@implementation NSString (NBL)

+ (NSString *)format:(NSInteger)integer
{
    return [NSString stringWithFormat:@"%@", @(integer)];
}

- (void)call
{
    NSString *urlString = [@"tel://" stringByAppendingString:self];
    [[NSURL URLWithString:urlString] openURL];
}
- (void)openSafari
{
    [[NSURL URLWithString:self] openURL];
}
- (void)openAppStore
{
    NSString *urlString = [@"https://itunes.apple.com/app/id" stringByAppendingString:self];
    [[NSURL URLWithString:urlString] openURL];
}
- (void)rate
{
    NSString *urlString = [NSString stringWithFormat:@"https://itunes.apple.com/app/id%@?mt=8&action=write-review", self];
    [[NSURL URLWithString:urlString] openURL];
}

- (UIColor *)color
{
    return [UIColor colorWithHexString:self];
}

- (NSDate *)date
{
    return [self dateWithFormat:@"yyyy-MM-dd HH:mm:ss"];
}
- (NSDate *)dateWithFormat:(NSString *)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = format;
    NSDate *date = [dateFormatter dateFromString:self];
    return date;
}

- (CGFloat)pixelWidthWithFont:(UIFont *)font
{
    CGRect rect = [self boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 100)  options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil];
    return rect.size.width;
}
- (CGFloat)pixelHeightWithFont:(UIFont *)font andMaxWidth:(CGFloat)maxWidth
{
    CGRect rect = [self boundingRectWithSize:CGSizeMake(maxWidth, 1000)  options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil];
    return rect.size.height;
}

@end
